package com.demon.androidbasic.base

import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.lifecycle.LifecycleObserver
import com.demon.androidbasic.R
import com.demon.androidbasic.titlebar.CommonTitleBar
import com.demon.androidbasic.titlebar.ITitleBar
import com.demon.logger.Logger
import com.demon.net.RpcCallManager
import com.gyf.immersionbar.BarHide.FLAG_SHOW_BAR
import com.gyf.immersionbar.ktx.immersionBar
import io.reactivex.Flowable
import io.reactivex.subscribers.ResourceSubscriber

/**
 * Fragment 基类
 *
 * Author : zhaozeyang
 * Date : 2020/8/21
 */
abstract class BaseFragment(layoutId: Int) : ImmersionFragment(layoutId), RpcCallManager {
    var mTitleBar: ITitleBar? = null
    private val rpcCallManager: RpcCallManager = RpcCallManager.RpcCallManagerImpl()
    protected lateinit var mContext: Context

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        // 添加到生命周期管理
        if (rpcCallManager is LifecycleObserver) {
            lifecycle.addObserver(rpcCallManager)
        }
        if (activity != null && activity?.isFinishing == true) {
            return
        }
    }

    fun setNavigationView(resId: Int? = R.mipmap.icon_back, clickEvent: (() -> Unit)? = null) {
        resId?.apply {
            mTitleBar?.getBackBtn()?.setImageResource(resId)
        }
        mTitleBar?.getBackBtn()?.setOnClickListener {
            clickEvent?.invoke() ?: activity?.onBackPressed()
        }
    }

    private fun onBackPressedDefault() {
    }

    override fun initImmersionBar() {
        immersionBar {
            fitsSystemWindows(true)
//            statusBarColors(getStatusBarColor())
//            statusBarDarkFont(true)
            hideBar(FLAG_SHOW_BAR)
            autoDarkModeEnable(true)
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mContext = requireContext()
        initTitleBar(view)
        setNavigationView()
    }

//    open fun getStatusBarColor() = R.color.bg_title_bar_color

    private fun initTitleBar(view: View) {
        val toolbar = view.findViewById<Toolbar>(R.id.title_bar_id)
        toolbar?.apply {
            mTitleBar = CommonTitleBar(this)
            (activity as AppCompatActivity).setSupportActionBar(this)
        }
    }

    fun setTitleBarTitle(title: String?) {
        mTitleBar?.getTitleView()?.text = title
    }

    fun setTitleBarTitle(titleId: Int) {
        mTitleBar?.getTitleView()?.setText(titleId)
    }

    inline fun setRightFirstImgBtn(resId: Int, crossinline clickEvent: (() -> Unit)) {
        mTitleBar?.getImageRightBtnFirst()?.apply {
            visibility = View.VISIBLE
            setImageResource(resId)
            setOnClickListener {
                clickEvent.invoke()
            }
        }

    }

    inline fun setRightSecondImgBtn(resId: Int, crossinline clickEvent: (() -> Unit)) {
        mTitleBar?.getImageRightBtnSecond()?.apply {
            visibility = View.VISIBLE
            setImageResource(resId)
            setOnClickListener {
                clickEvent.invoke()
            }
        }
    }

    open fun setToolbarElevation(elevation: Float) {
        mTitleBar?.setToolbarElevation(elevation)
    }

    override fun <T> manageRpcCall(flowable: Flowable<T>, subscribe: ResourceSubscriber<T>) {
        rpcCallManager.manageRpcCall(flowable, subscribe)
    }

    override fun onPause() {
        super.onPause()
        Logger.i(TAG, "$this fragment onPause")
    }

    override fun onStop() {
        super.onStop()
        Logger.i(TAG, "$this fragment onStop")
    }

    override fun onResume() {
        super.onResume()
        Logger.i(TAG, "$this fragment onResume")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Logger.i(TAG, "$this  fragment onDestroyView ")
    }

    override fun onDestroy() {
        super.onDestroy()
        Logger.i(TAG, "$this  fragment onDestroy ")
    }

    companion object {
        private const val TAG = "BaseFragment"
    }
}